package daos;

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.MasterNotRunningException;
import org.apache.hadoop.hbase.ZooKeeperConnectionException;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.client.HConnection;
import org.apache.hadoop.hbase.client.HConnectionManager;
import org.apache.hadoop.hbase.client.HTable;

public final class HBaseSessionManager {
	public static HTable table = null;
	
	public synchronized static Configuration getHBaseConfiguration(){
		Configuration config = HBaseConfiguration.create();
		config.clear();
		config.set("hbase.zookeeper.quorum", "127.0.0.1");
		
		return config;
	}
		
		public static HBaseAdmin HBaseSession(){
			HBaseAdmin hbAdmin=null;
			HConnection connection =null;
			try {
				connection = HConnectionManager.createConnection(getHBaseConfiguration());
				hbAdmin = new HBaseAdmin(connection);
			} catch (MasterNotRunningException e) {

				e.printStackTrace();
			} catch (ZooKeeperConnectionException e) {

				e.printStackTrace();
			} catch (IOException e) {

				e.printStackTrace();
			}
			return hbAdmin;
		}
		
		
		
		
		public static final HTable perfilPublicoTable() {
				
				if(table==null){
			
				try {
					table = new HTable(getHBaseConfiguration(),"PerfilPublico");
					
					HBaseSession().disableTable("PerfilPublico");
					HBaseSession().addColumn("PerfilPublico",new HColumnDescriptor("Imagen"));
					HBaseSession().enableTable("PerfilPublico");
					HBaseSession().close();	
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			return table;
		}
		
}
		
/*

public static void main(String args[]){
	Usuario userA = new Usuario("Roman","Rizzi", "rr", new Date(0), "rizzi@rizzi.com", "rom12");
	PerfilPublico perPub = new PerfilPublico(userA);
	try {
		
		TableOp op= new TableOp();
		op.create("PerfilPublico");
		HTable table= new HTable(getHBaseConfiguration(),"PerfilPublico");
		//createTablePerfilPublico();
		System.out.println(HBaseSession().tableExists("PerfilPublico"));
		AlmacenarPerfilEnCache al= new AlmacenarPerfilEnCache();
		al.save(perPub,table);
		System.out.println("Pasó");
		//HBaseSession().addColumn(table.getName(), new HColumnDescriptor(userA.getNombreDeUsuario()));
		File file= new File("/home/memonono/Fiesta/IMAG0441.jpg");
		op.savePhoto(file, Visibilidad.PUBLICO, userA, table.getName().toString());
		System.out.println("Pasó");
		op.getImage(userA, "IMAG0441.jpg", table.getName().toString());
		
		
		ConsultarPerfilEnCache pp= new ConsultarPerfilEnCache();
		System.out.println(pp.getPerfilPublico("rr", table).getUser().getApellido());
		op.delete("PerfilPublico");
		System.out.println(HBaseSession().tableExists("PerfilPublico"));
		
		
	} catch (MasterNotRunningException e) {
		
		e.printStackTrace();
	} catch (ZooKeeperConnectionException e) {
		
		e.printStackTrace();
	} catch (IOException e) {
		
		e.printStackTrace();
	}
}
*/		

